% Author: Feng Sheng
% Study: Buysell
% Design: Within-subject; Each subject makes 10*10=100 decisions in a block
% when playing either the role of a buyer or the role of a seller

%% Aim
% Calculate gaze information for each trial based on gaze.csv

%% Setup
tic;
clc;
clear;
root_path = pwd; 
addpath(genpath(root_path));

nrole=2;
nblock=2;
ntrial=100;
nprice=10;
nlottery=10;

nchoice=2;
choice_label={'Buy','Not Buy'; 'Sell','Not Sell'};

role_label={'Buyer','Seller'};

%% Load data
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
gaze_data=readtable([root_path,'\data\gaze\gaze.csv']);
eye_data=readtable([root_path,'\data\eye\eye.csv']);
sub_data=readtable([root_path,'\data\subject\subject.csv']);
nsub=height(sub_data);

%% Basic columns
Subject = beh_data.Subject;
Block = beh_data.Block;
Role = beh_data.Role;
PricePosition = beh_data.PricePosition;
Price = beh_data.Price;
Lottery = beh_data.Lottery;
Choice = beh_data.Choice;
RT = beh_data.RT;

%% Create fixation-level variables
PreGaze = NaN(length(Price),1);
PreGazeLottery = NaN(length(Price),1);
GazeNumPrice = NaN(length(Price),1);
GazeNumLottery = NaN(length(Price),1);
GazeDurPrice = NaN(length(Price),1);
GazeDurLottery = NaN(length(Price),1);
FirstGazeLottery = NaN(length(Price),1);
FirstGazeStart = NaN(length(Price),1);
FirstGazeEnd = NaN(length(Price),1);
LastGazeLottery = NaN(length(Price),1);
LastGazeStart = NaN(length(Price),1);
LastGazeEnd = NaN(length(Price),1);

for isub = 1:nsub 
    for irole = 1:2
        pricePosition = PricePosition(Subject == isub & Role == irole,:);
        gdata = gaze_data(gaze_data.Subject == isub & gaze_data.Role == irole,:);
        edata = eye_data(eye_data.Subject == isub & eye_data.Role == irole,:);
        stims = unique(gdata.StimulusName);
        stims = stims(stims > 100);
        
        for i = 1:length(stims)
            stim_idx = find(gdata.StimulusName == stims(i));
            
            % PreGaze
            %
            if stim_idx(1)==1
                % There should be pregaze data
                pregazeloc=NaN;
                pregazeloclottery=NaN;
            elseif gdata.TimeSignal(stim_idx(1)-1)+gdata.GazeTime(stim_idx(1)-1) < gdata.TimeSignal(stim_idx(1))-500
                % PreGaze should be within the baseline time windown (-500 ms to 0 ms)
                pregazeloc=NaN;
                pregazeloclottery=NaN;
            else
                pregazeloc = gdata.Location(stim_idx(1) - 1);
                if pricePosition == 1
                    if pregazeloc == -1
                        pregazeloclottery = 0;
                    elseif pregazeloc == 1
                        pregazeloclottery = 1;
                    else
                        pregazeloclottery = NaN;
                    end
                else
                    if pregazeloc == -1
                        pregazeloclottery = 1;
                    elseif pregazeloc == 1
                        pregazeloclottery = 0;
                    else
                        pregazeloclottery = NaN;
                    end
                end
            end
            
            % Gaze during gamble onset
            %
            gdata_stim = gdata(gdata.StimulusName == stims(i),:);
            gdata_stim = gdata_stim(gdata_stim.Location == 1 | gdata_stim.Location == -1,:);
            gazenumprice = 0;
            gazenumlottery = 0;
            gazepricedur = 0;
            gazelotterydur = 0;
            firstgazelottery = NaN;
            firstgazestart = NaN;
            firstgazeend = NaN;
            lastgazeloss = NaN;
            lastgazestart = NaN;
            lastgazeend = NaN;
            % loop through gaze within stims (on price or lottery only)
            for m = 1:height(gdata_stim)
                if pricePosition==1
                    if gdata_stim.Location(m) == -1
                        gazeOnPrice = true;
                    elseif gdata_stim.Location(m) == 1
                        gazeOnPrice = false;
                    end
                else
                    if gdata_stim.Location(m) == -1
                        gazeOnPrice = false;
                    elseif gdata_stim.Location(m) == 1
                        gazeOnPrice = true;
                    end
                end
                
                if gazeOnPrice
                    gazenumprice = gazenumprice + 1;
                    gazepricedur = gazepricedur + gdata_stim.GazeTime(m);
                    if m == 1
                        startoftrial = edata(edata.StimulusName == stims(i),:).TimeSignal(1);
                        firstgazestart = gdata_stim.TimeSignal(m) - startoftrial;
                        firstgazeend = firstgazestart + gdata_stim.GazeTime(1);
                        firstgazelottery = 0;
                    end
                    if m == height(gdata_stim)
                        endoftrial = edata(edata.StimulusName == stims(i),:).TimeSignal(end);
                        lastgazestart = gdata_stim.TimeSignal(m) - endoftrial;
                        lastgazeend = gdata_stim.TimeSignal(m) + gdata_stim.GazeTime(m) - endoftrial;
                        lastgazeloss = 0;
                    end
                else
                    gazenumlottery = gazenumlottery + 1;
                    gazelotterydur = gazelotterydur + gdata_stim.GazeTime(m);
                    if m == 1
                        startoftrial = edata(edata.StimulusName == stims(i),:).TimeSignal(1);
                        firstgazestart = gdata_stim.TimeSignal(m) - startoftrial;
                        firstgazeend = firstgazestart + gdata_stim.GazeTime(1);
                        firstgazelottery = 1;
                    end
                    if m == height(gdata_stim)
                        endoftrial = edata(edata.StimulusName == stims(i),:).TimeSignal(end);
                        lastgazestart = gdata_stim.TimeSignal(m) - endoftrial;
                        lastgazeend = gdata_stim.TimeSignal(m) + gdata_stim.GazeTime(m) - endoftrial;
                        lastgazeloss = 1;
                    end
                end
            end
            
            % assign to variables
            price = floor(stims(i)/1000);
            lottery = mod(stims(i),1000);
            idx = (Subject == isub & ...
                   Role == irole & ...
                   Price == price & ...
                   Lottery == lottery);
            PreGaze(idx) = pregazeloc;
            PreGazeLottery(idx) = pregazeloclottery;
            GazeNumPrice(idx) = gazenumprice;
            GazeNumLottery(idx) = gazenumlottery;
            GazeDurPrice(idx) = gazepricedur;
            GazeDurLottery(idx) = gazelotterydur;
            FirstGazeLottery(idx) = firstgazelottery;
            FirstGazeStart(idx) = firstgazestart;
            FirstGazeEnd(idx) = firstgazeend;
            LastGazeLottery(idx) = lastgazeloss;
            LastGazeStart(idx) = lastgazestart;
            LastGazeEnd(idx) = lastgazeend;
        end
    end
end

%% calculate variables that rely on other variables
GazeNum = GazeNumPrice + GazeNumLottery;
GazeNumLotteryRatio = GazeNumLottery ./ GazeNum;
GazeNumPriceRatio = GazeNumPrice ./ GazeNum;

FirstGazeDur = FirstGazeEnd - FirstGazeStart;
LastGazeDur = LastGazeEnd - LastGazeStart;

GazeDur = GazeDurPrice + GazeDurLottery;
GazeDurPriceRatio = GazeDurPrice ./ GazeDur;
GazeDurLotteryRatio = GazeDurLottery ./ GazeDur;

PerGaze = GazeDur ./GazeNum;
PerGazePrice = GazeDurPrice ./ GazeNumPrice;
PerGazeLottery = GazeDurLottery ./GazeNumLottery;
PerGazeLotteryPriceRatio = PerGazeLottery ./PerGazePrice;

%% create a table .csv for all subjects
gaze_trial_data = table(Subject, Block, Role, PricePosition, Price, Lottery, Choice, RT, ...
             PreGaze, PreGazeLottery, ...
             GazeNumPrice, GazeNumLottery, GazeNum, GazeNumLotteryRatio, ...
             GazeDurPrice, GazeDurLottery, GazeDur, GazeDurPriceRatio, GazeDurLotteryRatio, ... 
             PerGazePrice, PerGazeLottery, PerGaze, PerGazeLotteryPriceRatio, ...
             FirstGazeLottery, FirstGazeStart, FirstGazeEnd, FirstGazeDur, ...
             LastGazeLottery, LastGazeStart, LastGazeEnd, LastGazeDur);
writetable(gaze_trial_data, strcat(root_path, '\data\gaze\gaze_trial.csv'));
toc;
